package com.example.mvp.view

import android.content.Context
import android.content.res.TypedArray
import android.support.v4.content.ContextCompat
import android.text.TextUtils
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.example.baselibrary.R
import java.lang.Exception

/**
 * 公共的顶部栏TopBar
 * Created on 2017/11/24.
 *
 * @author chaohaipeng
 */

class SKTopBar : RelativeLayout {

    /**
     * 上下文
     */
    private var mContext: Context? = null

    /** 父容器 */
    /** 获取父容器控件 */
    var rl_ljtb_layout: RelativeLayout? = null
        private set

    /**
     * 布局左侧按钮容器
     */
    /** 获取布局左侧按钮容器控件 */
    var rl_ljtb_left_layout: LinearLayout? = null
        private set

    /**
     * 布局左侧图标控件
     */
    /** 获取布局左侧图标控件 */
    var iv_ljtb_left_back: ImageView? = null
        private set

    /**
     * 布局左侧文本控件
     */
    /** 获取布局左侧文本控件 */
    var tv_ljtb_left_back: TextView? = null
        private set

    /**
     * 布局中间标题文本控件
     */
    /** 获取布局中间标题文本控件 */
    var tv_ljtb_title: TextView? = null
        private set

    /**
     * 布局右侧按钮容器
     */
    /** 获取布局右侧按钮容器控件 */
    var rl_ljtb_right_layout: LinearLayout? = null
        private set

    /**
     * 布局右侧图标控件
     */
    /** 获取布局右侧图标控件 */
    var iv_ljtb_right_back: ImageView? = null
        private set

    /**
     * 布局右侧文本控件
     */
    /**
     * 设置右侧按钮文本内容
     */
    var rightTV: TextView? = null
        private set


  private  var lineView: View? = null
    private  var iv_ljtb_bottom_v: ImageView? = null


    /**
     * 布局底部分割线控件
     */
    /** 获取布局底部分割线控件 */
    val view_ljtb_divider: View? = null

    /**
     * 布局控件事件接口对象
     */
    private var eventInterface: EventInterface? = null

    /**
     * 标题箭头按钮
     */
    private var eventBottomInterface: EventBottomInterface? = null


    constructor(context: Context) : super(context) {
        mContext = context
        initView()
        initViewEvent()
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        mContext = context
        initView()
        initViewEvent()
        handleAttrs(attrs, 0)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        mContext = context
        initView()
        initViewEvent()
        handleAttrs(attrs, defStyleAttr)
    }

    /**
     * 初始化布局控件
     */
    private fun initView() {
        if (mContext == null) {
            return
        }
        val view = LayoutInflater.from(mContext).inflate(R.layout.layout_top_bar, null)
        rl_ljtb_layout = view.findViewById<View>(R.id.rl_ljtb_top_layout) as RelativeLayout
        tv_ljtb_title = view.findViewById<View>(R.id.tv_ljtb_title) as TextView
        rl_ljtb_left_layout = view.findViewById<View>(R.id.rl_ljtb_left_layout) as LinearLayout
        iv_ljtb_left_back = view.findViewById<View>(R.id.iv_ljtb_left_back) as ImageView
        tv_ljtb_left_back = view.findViewById<View>(R.id.tv_ljtb_left_back) as TextView
        rl_ljtb_right_layout = view.findViewById<View>(R.id.rl_ljtb_right_layout) as LinearLayout
        iv_ljtb_right_back = view.findViewById<View>(R.id.iv_ljtb_right_back) as ImageView
        rightTV = view.findViewById<View>(R.id.tv_ljtb_right_back) as TextView
        lineView = view.findViewById<View>(R.id.v_topbar_line)
        iv_ljtb_bottom_v = view.findViewById<ImageView>(R.id.iv_ljtb_bottom_v)
        addView(view)
    }

    /**
     * 设置标题下拉按钮是否选中
     */
    fun setImageSelector(isSelector:Boolean){
        if (isSelector){
            iv_ljtb_bottom_v?.isSelected=true
        }else{
            iv_ljtb_bottom_v?.isSelected=false
        }

    }


    /**
     * 隐藏底部线
     */
     fun inVisibleLine(){
        lineView?.visibility=View.GONE

    }
    /**
     * 显示底按钮
     */
    fun inVisibleBottom(){
        iv_ljtb_bottom_v?.visibility=View.VISIBLE

    }



    /**
     * 初始化布局控件事件
     */
    private fun initViewEvent() {
        if (mContext == null) {
            return
        }
        if (eventInterface != null) {
            if (rl_ljtb_left_layout != null) {
                rl_ljtb_left_layout!!.setOnClickListener {
                    if (iv_ljtb_left_back!!.visibility == View.VISIBLE || tv_ljtb_left_back!!.visibility == View.VISIBLE) {
                        eventInterface!!.leftOnClick()
                    }
                }

            }
            if (rl_ljtb_right_layout != null) {
                rl_ljtb_right_layout!!.setOnClickListener {
                    if (iv_ljtb_right_back!!.visibility == View.VISIBLE || rightTV!!.visibility == View.VISIBLE) {
                        eventInterface!!.rightOnClick()
                    }
                }

            }
        }
        if (eventBottomInterface != null) {
            if (tv_ljtb_title != null) {
                tv_ljtb_title!!.setOnClickListener {
                    if (iv_ljtb_bottom_v?.visibility==View.VISIBLE){
                        eventBottomInterface!!.openOnClick()
                    }
                }

            }
        }




    }

    private fun handleAttrs(attrs: AttributeSet?, defStyleAttr: Int) {
        if (mContext == null) {
            return
        }
        var typedArray: TypedArray?
        try {
            typedArray = mContext!!.obtainStyledAttributes(attrs, R.styleable.SKTopBar, defStyleAttr, 0)
        } catch (e: Exception) {
            e.printStackTrace()
            typedArray = null
        }

        if (typedArray != null) {
            setLayoutBGColor(typedArray.getColor(R.styleable.SKTopBar_jniTopBar_layoutBGColor, ContextCompat.getColor(mContext!!, R.color.color_FFFFFF)))

            setLeftBtnImage(typedArray.getResourceId(R.styleable.SKTopBar_jniTopBar_leftIVResources, R.mipmap.icon_back))
            setLeftBtnImageShow(typedArray.getBoolean(R.styleable.SKTopBar_jniTopBar_leftIVVisibility, true))
            setLeftTVContent(typedArray.getString(R.styleable.SKTopBar_jniTopBar_leftTVContent))
            setLeftTVColor(typedArray.getColor(R.styleable.SKTopBar_jniTopBar_leftTVColor, ContextCompat.getColor(mContext!!, R.color.color_474747)))
            setLeftTVFontSize(typedArray.getInteger(R.styleable.SKTopBar_jniTopBar_leftTVFontSize, 16))
            if (typedArray.getBoolean(R.styleable.SKTopBar_jniTopBar_leftTVFontBold, false)) {
                setLeftTVContentBold()
            }

            setTitle(typedArray.getString(R.styleable.SKTopBar_jniTopBar_titleContent))
            setTitleColor(typedArray.getColor(R.styleable.SKTopBar_jniTopBar_titleColor, ContextCompat.getColor(mContext!!, R.color.color_474747)))
            setTitleFontSize(typedArray.getInteger(R.styleable.SKTopBar_jniTopBar_titleFontSize, 18))
            if (typedArray.getBoolean(R.styleable.SKTopBar_jniTopBar_titleFontBold, true)) {
                setTitleContentBold()
            }

            setRightBtnImage(typedArray.getResourceId(R.styleable.SKTopBar_jniTopBar_rightIVResources, 0))
            setRightBtnImageShow(typedArray.getBoolean(R.styleable.SKTopBar_jniTopBar_rightIVVisibility, false))
            setRightTVContent(typedArray.getString(R.styleable.SKTopBar_jniTopBar_rightTVContent))
            setRightTVColor(typedArray.getColor(R.styleable.SKTopBar_jniTopBar_rightTVColor, ContextCompat.getColor(mContext!!, R.color.color_474747)))
            setRightTVFontSize(typedArray.getInteger(R.styleable.SKTopBar_jniTopBar_rightTVFontSize, 16))
            if (typedArray.getBoolean(R.styleable.SKTopBar_jniTopBar_rightTVFontBold, false)) {
                setRightTVContentBold()
            }

            setWhetherShowDividerView(typedArray.getBoolean(R.styleable.SKTopBar_jniTopBar_dividerVisibility, true))
            typedArray.recycle()
        }
    }

    /**
     * 设置父容器背景颜色
     *
     * @param color 颜色的资源ID
     */
    fun setLayoutBGColor(color: Int) {
        if (rl_ljtb_layout == null) {
            return
        }
        rl_ljtb_layout!!.setBackgroundColor(color)
    }

    /**
     * 设置标题内容
     *
     * @param title 标题文字内容
     */
    fun setTitle(title: String?) {
        if (tv_ljtb_title == null) {
            return
        }
        if (TextUtils.isEmpty(title)) {
            tv_ljtb_title!!.visibility = View.GONE
        } else {
            tv_ljtb_title!!.visibility = View.VISIBLE
            tv_ljtb_title!!.text = title
        }
    }

    /**
     * 设置标题文字大小
     *
     * @param size 标题文字大小，默认转成sp
     */
    fun setTitleFontSize(size: Int) {
        if (tv_ljtb_title == null) {
            return
        }
        if (size > 0) {
            tv_ljtb_title!!.textSize = size.toFloat()
        }
    }

    /**
     * 设置标题字体颜色
     *
     * @param color 颜色的资源ID
     */
    fun setTitleColor(color: Int) {
        if (tv_ljtb_title == null) {
            return
        }
        tv_ljtb_title!!.setTextColor(color)
    }

    /**
     * 设置标题字体是否加粗
     */
    fun setTitleContentBold() {
        if (tv_ljtb_title == null) {
            return
        }
        val paint = tv_ljtb_title!!.paint
        paint.isFakeBoldText = true
    }

    /**
     * 设置左侧按钮图标
     *
     * @param res 图片的资源ID
     */
    fun setLeftBtnImage(res: Int) {
        if (iv_ljtb_left_back == null) {
            return
        }
        iv_ljtb_left_back!!.setImageResource(res)
        setLeftBtnImageShow(true)
    }

    /**
     * 设置左侧按钮图标显示状态
     *
     * @param show 图片控件显示状态
     */
    fun setLeftBtnImageShow(show: Boolean) {
        if (iv_ljtb_left_back == null) {
            return
        }
        if (show) {
            iv_ljtb_left_back!!.visibility = View.VISIBLE
            initViewEvent()
        } else {
            iv_ljtb_left_back!!.visibility = View.GONE
        }
    }

    /**
     * 设置左侧按钮文本内容
     *
     * @param content 文本文字内容
     */
    fun setLeftTVContent(content: String?) {
        if (tv_ljtb_left_back == null) {
            return
        }
        if (TextUtils.isEmpty(content)) {
            tv_ljtb_left_back!!.visibility = View.GONE
        } else {
            tv_ljtb_left_back!!.visibility = View.VISIBLE
            tv_ljtb_left_back!!.text = content
            initViewEvent()
        }
    }

    /**
     * 设置左侧按钮文本字体大小
     *
     * @param size 标题文字大小，默认转成sp
     */
    fun setLeftTVFontSize(size: Int) {
        if (tv_ljtb_left_back == null) {
            return
        }
        if (size > 0) {
            tv_ljtb_left_back!!.textSize = size.toFloat()
        }
    }

    /**
     * 设置左侧按钮文本字体颜色
     *
     * @param color 颜色的资源ID
     */
    fun setLeftTVColor(color: Int) {
        if (tv_ljtb_left_back == null) {
            return
        }
        tv_ljtb_left_back!!.setTextColor(color)
    }

    /**
     * 设置左侧侧文本控件字体加粗
     */
    fun setLeftTVContentBold() {
        if (tv_ljtb_left_back == null) {
            return
        }
        val paint = tv_ljtb_left_back!!.paint
        paint.isFakeBoldText = true
    }

    /**
     * 设置右侧按钮图标
     *
     * @param res 图片的资源ID
     */
    fun setRightBtnImage(res: Int) {
        if (iv_ljtb_right_back == null) {
            return
        }
        iv_ljtb_right_back?.setImageResource(res)
        setRightBtnImageShow(true)
    }

    /**
     * 设置右侧按钮图标显示状态
     *
     * @param show 图片控件显示状态
     */
    fun setRightBtnImageShow(show: Boolean) {
        if (iv_ljtb_right_back == null) {
            return
        }
        if (show) {
            iv_ljtb_right_back!!.visibility = View.VISIBLE
            initViewEvent()
        } else {
            iv_ljtb_right_back!!.visibility = View.GONE
        }
    }

    /**
     * 设置右侧按钮文本内容
     *
     * @param content 文本文字内容
     */
    fun setRightTVContent(content: String?) {
        if (rightTV == null) {
            return
        }
        if (TextUtils.isEmpty(content)) {
            rightTV!!.visibility = View.GONE
        } else {
            rightTV!!.visibility = View.VISIBLE
            rightTV!!.text = content
            initViewEvent()
        }
    }

    /**
     * 设置右侧按钮文本字体大小
     *
     * @param size 标题文字大小，默认转成sp
     */
    fun setRightTVFontSize(size: Int) {
        if (rightTV == null) {
            return
        }
        if (size > 0) {
            rightTV!!.textSize = size.toFloat()
        }
    }

    /**
     * 设置右侧按钮文本字体颜色
     *
     * @param color 颜色的资源ID
     */
    fun setRightTVColor(color: Int) {
        if (rightTV == null) {
            return
        }
        rightTV!!.setTextColor(color)
    }

    /**
     * 设置右侧文本控件字体加粗
     */
    fun setRightTVContentBold() {
        if (rightTV == null) {
            return
        }
        val paint = rightTV!!.paint
        paint.isFakeBoldText = true
    }

    /**
     * 设置是否显示底部分割线控件
     *
     * @param whetherShow 是否显示
     */
    fun setWhetherShowDividerView(whetherShow: Boolean) {
        if (view_ljtb_divider == null) {
            return
        }
        if (whetherShow) {
            view_ljtb_divider.visibility = View.VISIBLE
        } else {
            view_ljtb_divider.visibility = View.GONE
        }
    }

    /** 获取布局右侧文本控件 */
    fun getTv_ljtb_right_back(): TextView? {
        return rightTV
    }

    fun setEventInterface(event: EventInterface) {
        this.eventInterface = event
        initViewEvent()
    }
    fun setEventBottomInterface(event: EventBottomInterface) {
        this.eventBottomInterface = event
        initViewEvent()
    }
    /**
     * 布局控件事件接口
     */
    interface EventInterface {

        /**
         * 左侧按钮点击事件
         */
        fun leftOnClick()

        /**
         * 右侧按钮点击事件
         */
        fun rightOnClick()

    }

    /**
     * 布局控件事件接口
     */
    interface EventBottomInterface {

        /**
         * 打开按钮
         */
        fun openOnClick()

    }
}